import json
from django.shortcuts import HttpResponse
from service.company_node_llm_service import CompanyNodeLLMService
from service.shareholder_node_llm_service import ShareHolderNodeLLMService

#查询节点
def query_llm_shareholder_node(request):
    if request.method == 'GET':
        shareholder_node_llm_service = ShareHolderNodeLLMService()
        rs=shareholder_node_llm_service.query_node()
        if len(rs)>0:
            data={"code":200,"msg":"查询成功","data":rs}
        else:
            data={"code":500,"msg":"没查询到数据","data":None}
        return HttpResponse(json.dumps(data))

#创建节点
def create_llm_shareholder_node(request):
    if request.method == 'GET':
        shareholder_node_llm_service = ShareHolderNodeLLMService()
        #获取前端发送的请求
        name=request.GET.get('name') #"股东姓名/名称"
        id_no=request.GET.get('id_no') #"身份证号/统一信用代码"
        nationality=request.GET.get('nationality') #"国籍"
        type=request.GET.get('type') #"自然人|法人"
        is_controlled=request.GET.get('is_controlled') #是否控股
        try:
            shareholder_node_llm_service.create_node(
                name=name,
                id_no=id_no,
                nationality=nationality,
                type=type,
                is_controlled=is_controlled,
            )
            data={"code":200,"msg":"创建成功","data":None}
            return HttpResponse(json.dumps(data))
        except Exception as e:
            print(e)
            data={"code":500,"msg":"创建失败","data":None}
            return HttpResponse(json.dumps(data))

#更新节点
def update_llm_shareholder_node(request):
    if request.method == 'GET':
        shareholder_node_llm_service = ShareHolderNodeLLMService()
        # 获取前端发送的请求
        name = request.GET.get('name')  # "股东姓名/名称"
        id_no = request.GET.get('id_no')  # "身份证号/统一信用代码"
        nationality = request.GET.get('nationality')  # "国籍"
        type = request.GET.get('type')  # "自然人|法人"
        is_controlled = request.GET.get('is_controlled')  # 是否控股
        try:
            shareholder_node_llm_service.update_node(
                name=name,
                id_no=id_no,
                nationality=nationality,
                type=type,
                is_controlled=is_controlled,
            )
            data = {"code": 200, "msg": "更新成功", "data": None}
            return HttpResponse(json.dumps(data))
        except Exception as e:
            print(e)
            data = {"code": 500, "msg": "更新失败", "data": None}
            return HttpResponse(json.dumps(data))

#删除节点
def delete_llm_shareholder_node(request):
    if request.method == 'GET':
        shareholder_node_llm_service = ShareHolderNodeLLMService()
        name=request.GET.get('name')
        try:
            shareholder_node_llm_service.delete_node(name=name)
            data={"code":200,"msg":"删除成功","data":None}
            return HttpResponse(json.dumps(data))
        except Exception as e:
            print(e)
            data = {"code": 500, "msg": "删除失败", "data": None}
            return HttpResponse(json.dumps(data))

